$(function() {
	// new file button click
	$('.new-btn-group').delegate('a', 'click', function() {
		var $this = $(this);
		var uri = $this.data('uri');

		if (!uri) {
			alert('Can not find the create button\'s uri.');
			return;
		}

		if ($.suredy.showModal)
			$.suredy.showModal({
				showHead : false,
				showFoot : false,
				showOkBtn : false,
				showCancelBtn : false,
				text : '文件创建中……'
			});

		var responseUri = undefined;

		$.ajax({
			url : uri,
			async : false,
			success : function(response, textStatus, jqXHR) {
				if (!response) {
					alert('Create file error.');
					return;
				}

				responseUri = response;
			},
			error : function(jqXHR, textStatus, errorThrown) {
				alert('Create file error. ' + errorThrown);
			}
		});

		window.setTimeout(function() {
			if ($.suredy.closeModal)
				$.suredy.closeModal();
		}, 400);

		window.setTimeout(function() {
			if (responseUri) {
				var win = window.open('about:blank', '_blank');

				if (!win)
					alert('新建文档窗口被浏览器拦截\n请在浏览器设置中选择“始终允许”本站点的弹出窗口！');
				else
					win.location = responseUri;
			}
		}, 600);
	});
});